//	AUTHOR:			ENRICO CANTONI
//	CREATED:		SEPTEMBER 19, 2017
//	MODIFIED: 		JUNE 2, 2022
//	DESCRIPTION:	Creates the DD tables of the effect of marital status, kids.
//					Specifically, it creates:
//						- Table 2 (gender-specific effects of marriage)
//						- Table 3 (gender-specific effects of childrearing)
//						- Table A3 (marriage effects by election type)
//						- Table A4 (children's effects by parents' origin I)
//						- Table A5 (children's effects by parents' origin II)
//						- Table A6 (children's effects by cost of voting)
//						- Table A7 (children's effects by political context)


// Set Stata version
version 16.1

********************************************************************************
********************************************************************************

*** TABLE 2: TURNOUT EFFECT OF MARITAL STATUS BY VOTER'S GENDER ***

// Outcome
local Y			voted

// Main regressors
local X			marriedX* divorcedX* widowedX*

// Table name
local sheet		Table_2

// Controls by col
local Z_1		i.year
#delimit ;
local Z_2		year##nbhd
				precinct_age
				precinct_citizen
				precinct_female
				precinct_hh_income_mod
				hh_citizen
				hh_income_mod
				income
				irpef
;
local Z_3 		`Z_2'
				female##(anykids_0to5
						 anykids_6to11
						 anykids_12to17
						 anykids_18more)
;
#delimit cr
local Z_4		`Z_3'
local Z_5		`Z_3'

// Load data
use ${analysis_clean} if s_all

// Initialize matrix of results
local n_rows = 3
foreach v of varlist `X' {
	local n_rows = `n_rows'+3
}
local n_cols = 5
mat results = J(`n_rows', `n_cols', 0)

// Loop over cols
forvalues col = 1/`n_cols' {

	// Load data
	use ${analysis_clean} if s_all

	// Voters who are always in a poor family
	if `col' == 4 {
		fasterxtile temp = hh_income_mod, nquantiles(4) by(year)
		bysort ID_voter (temp): keep if temp[_N] <= 2
		drop temp
	}
	
	// Voters who are always in a rich family
	if `col' == 5 {
		fasterxtile temp = hh_income_mod, nquantiles(4) by(year)
		bysort ID_voter (temp): keep if temp[1] >= 3
		drop temp
	}

	// Run regression
	#delimit ;
	reghdfe `Y' `Z_`col'' `X',
		absorb(ID_voter female##age) cluster(ID_hh ID_voter)
	;
	#delimit cr

	// Initialize row counter
	local row = 1

	// Initialize "control" sample
	local s_control ""

	// Store estimates
	foreach v of varlist `X' {
		mat results[`row', `col'] = _b[`v']
		mat results[`row'+1, `col'] = _se[`v']

		// Define "control" sample
		local s_control "`s_control' & !`v'"

		// Increase col counter
		local row = `row'+2
	}

	// Store gender gap
	foreach str of local X {
		local pref = regexr("`str'", "\*$", "")
		lincom `pref'female - `pref'male
		mat results[`row', `col'] = r(estimate)
		mat results[`row'+1, `col'] = r(se)

		// Increase row counter
		local row = `row'+2
	}

	// Store control means
	foreach str in "female" "!female" {
		gstats sum `Y' if e(sample) `s_control' & `str', meanonly
		mat results[`row', `col'] = r(mean)
		local ++row
	}
	mat results[`row', `col'] = e(N)
	clear
}

// Export matrix
clear
matlist results
svmat results
export_excel using ${tables_raw}, sheet(`sheet') sheetreplace
clear

********************************************************************************
********************************************************************************

*** TABLE 3: TURNOUT EFFECT OF CHILDREN BY VOTER'S GENDER ***

// Outcome
local Y			voted

// Main regressors
local X			anykids_0to5X* anykids_6to11X* anykids_12to17X* anykids_18moreX*

// Table name
local sheet		Table_3

// Controls by col
local Z_1		i.year
#delimit ;
local Z_2		year##nbhd
				precinct_age
				precinct_citizen
				precinct_female
				precinct_hh_income_mod
				hh_citizen
				hh_income_mod
				income
				irpef
;
local Z_3 		`Z_2'
				female##maritalstatus
;
#delimit cr
local Z_4		`Z_3'
local Z_5		`Z_3'

// Load data
use ${analysis_clean} if s_kids

// Initialize matrix of results
local n_rows = 3
foreach v of varlist `X' {
	local n_rows = `n_rows'+3
}
local n_cols = 5
mat results = J(`n_rows', `n_cols', 0)

// Loop over cols
forvalues col = 1/`n_cols' {

	// Load data
	use ${analysis_clean} if s_kids

	// Individuals who never got married
	if `col' == 4 {
		gegen temp = max(maritalstatus != "Single":maritalstatus), by(ID_voter)
		keep if !temp
		drop temp
	}

	// Both parents born in the Center-Southern Italy and married at some point
	if `col' == 5 {
		gegen temp = min(inrange(birth_region, 9, 20)), by(ID_hh)
		gegen temp2 = min(temp), by(ID_voter)
		gegen temp3 = max(maritalstatus != "Single":maritalstatus), by(ID_voter)
		keep if temp2 & temp3
		drop temp*
	}

	// Run regression
	#delimit ;
	reghdfe `Y' `Z_`col'' `X',
		absorb(ID_voter female##age) cluster(ID_hh ID_voter)
	;
	#delimit cr

	// Initialize row counter
	local row = 1

	// Initialize "control" sample
	local s_control ""

	// Store estimates
	foreach v of varlist `X' {
		mat results[`row', `col'] = _b[`v']
		mat results[`row'+1, `col'] = _se[`v']

		// Define "control" sample
		local s_control "`s_control' & !`v'"

		// Increase col counter
		local row = `row'+2
	}

	// Store gender gap
	foreach str of local X {
		local pref = regexr("`str'", "\*$", "")
		lincom `pref'female - `pref'male
		mat results[`row', `col'] = r(estimate)
		mat results[`row'+1, `col'] = r(se)

		// Increase row counter
		local row = `row'+2
	}

	// Store control means
	foreach str in "female" "!female" {
		gstats sum `Y' if e(sample) `s_control' & `str', meanonly
		mat results[`row', `col'] = r(mean)
		local ++row
	}
	mat results[`row', `col'] = e(N)
	clear
}

// Export matrix
clear
matlist results
svmat results
export_excel using ${tables_raw}, sheet(`sheet') sheetreplace
clear

********************************************************************************
********************************************************************************

*** APPENDIX TABLE A3: MARITAL STATUS HETEROGENEITY BY ELECTION TYPE ***

// Samples
local s_1		s_all
local s_2		s_all & inlist(year, 2004, 2009)
local s_3		s_all & inlist(year, 2008, 2013)

// Outcome
local Y			voted

// Regressors of interest
local X			marriedX* divorcedX* widowedX*

// Covariates
#delimit ;
local covs
	year##nbhd
	precinct_age
	precinct_citizen
	precinct_female
	precinct_hh_income_mod
	hh_citizen
	hh_income_mod
	income
	irpef
	female##(anykids_0to5
			 anykids_6to11
			 anykids_12to17
			 anykids_18more)
;
#delimit cr

// Initialize matrix of results
use ${analysis_clean}
local n_cols = 3
local n_rows = 3
foreach v of varlist `X' {
	local n_rows = `n_rows'+3
}
mat results = J(`n_rows', `n_cols', 0)

// Table name
local table Table_A3

// Loop over samples
forvalues col = 1/`n_cols' {

	// Load data for given sample
	use ${analysis_clean} if `s_`col''

	// Run regression
	reghdfe `Y' `covs' `X', absorb(ID_voter female##age) cluster(ID_hh ID_voter)

	// Initialize row counter
	local row = 1
	
	// Initialize "control" sample
	local s_control ""

	// Store estimates
	foreach v of varlist `X' {
		mat results[`row', `col'] = _b[`v']
		mat results[`row'+1, `col'] = _se[`v']

		// Define "control" sample
		local s_control "`s_control' & !`v'"

		// Increase col counter
		local row = `row'+2
	}

	// Store gender gap
	foreach str of local X {
		local Xpref = regexr("`str'", "\*$", "")
		lincom `Xpref'female - `Xpref'male
		mat results[`row', `col'] = r(estimate)
		mat results[`row'+1, `col'] = r(se)

		// Increase row counter
		local row = `row'+2
	}

	// Store control means
	foreach str in "female" "!female" {
		sum `Y' if e(sample) `s_control' & `str'
		mat results[`row', `col'] = r(mean)
		local ++row
	}
	mat results[`row', `col'] = e(N)
	clear
}

// Export matrix
clear
matlist results
svmat results
export_excel using ${tables_raw}, sheet(`table') sheetreplace
clear

********************************************************************************
********************************************************************************

*** APPENDIX TABLE A4: CHILDREN'S HETEROGENEITY BY MARITAL STATUS AND ORIGIN ***

// Samples
local s_1		s_kids
local s_2		s_kids
local s_3		s_kids
local s_4		s_kids
local s_5		s_kids

// Outcome
local Y			voted

// Regressors of interest
local X			anykids_0to5X* anykids_6to11X* anykids_12to17X* anykids_18moreX*

// Covariates
#delimit ;
local covs
	year##nbhd
	precinct_age
	precinct_citizen
	precinct_female
	precinct_hh_income_mod
	hh_citizen
	hh_income_mod
	income
	irpef
	female##maritalstatus
;
#delimit cr

// Initialize matrix of results
use ${analysis_clean}
local n_cols = 5
local n_rows = 3
foreach v of varlist `X' {
	local n_rows = `n_rows'+3
}
mat results = J(`n_rows', `n_cols', 0)

// Table name
local table Table_A4

// Loop over samples
forvalues col = 1/`n_cols' {

	// Load data for given sample
	use ${analysis_clean} if `s_`col''

	// Voters who got married at some point
	if `col' == 2 {
		gegen temp = max(maritalstatus != "Single":maritalstatus), by(ID_voter)
		keep if temp
		drop temp
	}

	// Voters who never got married
	if `col' == 3 {
		gegen temp = max(maritalstatus != "Single":maritalstatus), by(ID_voter)
		keep if !temp
		drop temp
	}

	// Both parents born in Northern Italy and married at some point
	if `col' == 4 {
		gegen temp = min(birth_region <= 8), by(ID_hh)
		gegen temp2 = min(temp), by(ID_voter)
		gegen temp3 = max(maritalstatus != "Single":maritalstatus), by(ID_voter)
		keep if temp2 & temp3
		drop temp*
	}

	// Both parents born in the Center-Southern Italy and married at some point
	if `col' == 5 {
		gegen temp = min(inrange(birth_region, 9, 20)), by(ID_hh)
		gegen temp2 = min(temp), by(ID_voter)
		gegen temp3 = max(maritalstatus != "Single":maritalstatus), by(ID_voter)
		keep if temp2 & temp3
		drop temp*
	}

	// Run regression
	reghdfe `Y' `covs' `X', absorb(ID_voter female##age) cluster(ID_hh ID_voter)

	// Initialize row counter
	local row = 1
	
	// Initialize "control" sample
	local s_control ""

	// Store estimates
	foreach v of varlist `X' {
		mat results[`row', `col'] = _b[`v']
		mat results[`row'+1, `col'] = _se[`v']

		// Define "control" sample
		local s_control "`s_control' & !`v'"

		// Increase col counter
		local row = `row'+2
	}
	
	// Store gender gap
	foreach str of local X {
		local Xpref = regexr("`str'", "\*$", "")
		lincom `Xpref'female - `Xpref'male
		mat results[`row', `col'] = r(estimate)
		mat results[`row'+1, `col'] = r(se)

		// Increase row counter
		local row = `row'+2
	}

	// Store control means
	foreach str in "female" "!female" {
		sum `Y' if e(sample) `s_control' & `str'
		mat results[`row', `col'] = r(mean)
		local ++row
	}
	mat results[`row', `col'] = e(N)
	clear
}

// Export matrix
clear
matlist results
svmat results
export_excel using ${tables_raw}, sheet(`table') sheetreplace
clear

********************************************************************************
********************************************************************************

*** APPENDIX TABLE A5: CHILDREN'S HETEROGENEITY BY PARENTAL REGION OF ORIGIN ***

// Samples
local s_1		s_kids
local s_2		s_kids
local s_3		s_kids
local s_4		s_kids
local s_5		s_kids

// Outcome
local Y			voted

// Regressors of interest
local X			anykids_0to5X* anykids_6to11X* anykids_12to17X* anykids_18moreX*

// Covariates
#delimit ;
local covs
	year##nbhd
	precinct_age
	precinct_citizen
	precinct_female
	precinct_hh_income_mod
	hh_citizen
	hh_income_mod
	income
	irpef
	female##maritalstatus
;
#delimit cr

// Initialize matrix of results
use ${analysis_clean}
local n_cols = 5
local n_rows = 3
foreach v of varlist `X' {
	local n_rows = `n_rows'+3
}
mat results = J(`n_rows', `n_cols', 0)

// Table name
local table Table_A5

// Loop over samples
forvalues col = 1/`n_cols' {

	// Load data for given sample
	use ${analysis_clean} if `s_`col''

	// Both parents born in the Center-Southern Italy and married at some point
	if `col' == 2 {
		gegen temp = min(inrange(birth_region, 9, 20)), by(ID_hh)
		gegen temp2 = min(temp), by(ID_voter)
		gegen temp3 = max(maritalstatus != "Single":maritalstatus), by(ID_voter)
		keep if temp2 & temp3
		drop temp*
	}

	// At least one parent born in the Center-South of Italy
	if `col' == 3 {
		gegen temp = max(inrange(birth_region, 9, 20)), by(ID_hh)
		gegen temp2 = min(temp), by(ID_voter)
		gegen temp3 = max(maritalstatus != "Single":maritalstatus), by(ID_voter)
		keep if temp2 & temp3
		drop temp*
	}

	// Mother born in the Center-South of Italy
	if `col' == 4 {
		gegen temp = max(inrange(birth_region, 9, 20) & female), by(ID_hh)
		gegen temp2 = min(temp), by(ID_voter)
		gegen temp3 = max(maritalstatus != "Single":maritalstatus), by(ID_voter)
		keep if temp2 & temp3
		drop temp*
	}

	// Father born in the Center-South of Italy
	if `col' == 5 {
		gegen temp = max(inrange(birth_region, 9, 20) & !female), by(ID_hh)
		gegen temp2 = min(temp), by(ID_voter)
		gegen temp3 = max(maritalstatus != "Single":maritalstatus), by(ID_voter)
		keep if temp2 & temp3
		drop temp*
	}

	// Run regression
	reghdfe `Y' `covs' `X', absorb(ID_voter female##age) cluster(ID_hh ID_voter)

	// Initialize row counter
	local row = 1
	
	// Initialize "control" sample
	local s_control ""

	// Store estimates
	foreach v of varlist `X' {
		mat results[`row', `col'] = _b[`v']
		mat results[`row'+1, `col'] = _se[`v']

		// Define "control" sample
		local s_control "`s_control' & !`v'"

		// Increase col counter
		local row = `row'+2
	}
	
	// Store gender gap
	foreach str of local X {
		local Xpref = regexr("`str'", "\*$", "")
		lincom `Xpref'female - `Xpref'male
		mat results[`row', `col'] = r(estimate)
		mat results[`row'+1, `col'] = r(se)

		// Increase row counter
		local row = `row'+2
	}

	// Store control means
	foreach str in "female" "!female" {
		sum `Y' if e(sample) `s_control' & `str'
		mat results[`row', `col'] = r(mean)
		local ++row
	}
	mat results[`row', `col'] = e(N)
	clear
}

// Export matrix
clear
matlist results
svmat results
export_excel using ${tables_raw}, sheet(`table') sheetreplace
clear

********************************************************************************
********************************************************************************

*** APPENDIX TABLE A6: OTHER HETEROGENEITY, KIDS (DISTANCE & MAX 1 KID) ***

// Samples
local s_1		s_kids
local s_2		s_kids & inrange(distance, 0, 400)
local s_3		s_kids & inrange(distance, 400, 10000)
local s_4		s_kids
local s_5		s_kids
local s_6		s_kids

// Outcome
local Y			voted

// Regressors of interest
local X			anykids_0to5X* anykids_6to11X* anykids_12to17X* anykids_18moreX*

// Covariates
#delimit ;
local covs
	year##nbhd
	precinct_age
	precinct_citizen
	precinct_female
	precinct_hh_income_mod
	hh_citizen
	hh_income_mod
	income
	irpef
	female##maritalstatus
;
#delimit cr

// Initialize matrix of results
use ${analysis_clean}
local n_cols = 4
local n_rows = 3
foreach v of varlist `X' {
	local n_rows = `n_rows'+3
}
mat results = J(`n_rows', `n_cols', 0)

// Table name
local table Table_A6

// Loop over samples
forvalues col = 1/`n_cols' {

	// Load data for given sample
	use ${analysis_clean} if `s_`col''

	// Max 1 kid in 0-18 age range
	if `col' == 4 {
		gegen temp = max(nkids_0to18 == 1), by(ID_voter)
		keep if temp
		drop temp
	}

	// Run regression
	reghdfe `Y' `covs' `X', absorb(ID_voter female##age) cluster(ID_hh ID_voter)

	// Initialize row counter
	local row = 1
	
	// Initialize "control" sample
	local s_control ""

	// Store estimates
	foreach v of varlist `X' {
		mat results[`row', `col'] = _b[`v']
		mat results[`row'+1, `col'] = _se[`v']

		// Define "control" sample
		local s_control "`s_control' & !`v'"

		// Increase col counter
		local row = `row'+2
	}

	// Store gender gap
	foreach str of local X {
		local Xpref = regexr("`str'", "\*$", "")
		lincom `Xpref'female - `Xpref'male
		mat results[`row', `col'] = r(estimate)
		mat results[`row'+1, `col'] = r(se)

		// Increase row counter
		local row = `row'+2
	}

	// Store control means
	foreach str in "female" "!female" {
		sum `Y' if e(sample) `s_control' & `str'
		mat results[`row', `col'] = r(mean)
		local ++row
	}
	mat results[`row', `col'] = e(N)
	clear
}

// Export matrix
clear
matlist results
svmat results
export_excel using ${tables_raw}, sheet(`table') sheetreplace
clear

********************************************************************************
********************************************************************************

*** APPENDIX TABLE A7: KIDS' EFFECTS BY ELECTION TYPE & LEFT VOTE SHARE ***

// Samples
local s_1		s_kids
local s_2		s_kids & inlist(year, 2004, 2009)
local s_3		s_kids & inlist(year, 2008, 2013)
local s_4		s_kids
local s_5		s_kids

// Outcome
local Y			voted

// Regressors of interest
local X			anykids_0to5X* anykids_6to11X* anykids_12to17X* anykids_18moreX*

// Covariates
#delimit ;
local covs
	year##nbhd
	precinct_age
	precinct_citizen
	precinct_female
	precinct_hh_income_mod
	hh_citizen
	hh_income_mod
	income
	irpef
	female##maritalstatus
;
#delimit cr

// Initialize matrix of results
use ${analysis_clean}
local n_cols = 5
local n_rows = 3
foreach v of varlist `X' {
	local n_rows = `n_rows'+3
}
mat results = J(`n_rows', `n_cols', 0)

// Table name
local table Table_A7

// Loop over samples
forvalues col = 1/`n_cols' {

	// Load data for given sample
	use ${analysis_clean} if `s_`col''

	// Precincts with above-median 2008 center-left's vote share
	if `col' == 4 {
		gstats sum voteshare_centerleft
		keep if voteshare_centerleft > r(p50)
	}

	// Precincts with below-median 2008 center-left's vote share
	if `col' == 5 {
		gstats sum voteshare_centerleft
		keep if voteshare_centerleft <= r(p50)
	}

	// Run regression
	reghdfe `Y' `covs' `X', absorb(ID_voter female##age) cluster(ID_hh ID_voter)

	// Initialize row counter
	local row = 1
	
	// Initialize "control" sample
	local s_control ""

	// Store estimates
	foreach v of varlist `X' {
		mat results[`row', `col'] = _b[`v']
		mat results[`row'+1, `col'] = _se[`v']

		// Define "control" sample
		local s_control "`s_control' & !`v'"

		// Increase col counter
		local row = `row'+2
	}

	// Store gender gap
	foreach str of local X {
		local Xpref = regexr("`str'", "\*$", "")
		lincom `Xpref'female - `Xpref'male
		mat results[`row', `col'] = r(estimate)
		mat results[`row'+1, `col'] = r(se)

		// Increase row counter
		local row = `row'+2
	}

	// Store control means
	foreach str in "female" "!female" {
		sum `Y' if e(sample) `s_control' & `str'
		mat results[`row', `col'] = r(mean)
		local ++row
	}
	mat results[`row', `col'] = e(N)
	clear
}

// Export matrix
clear
matlist results
svmat results
export_excel using ${tables_raw}, sheet(`table') sheetreplace
clear
